//
//  SISSCooperationDetailViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/19.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSCooperationDetailViewController.h"
#import "SISSTextView.h"
#import "SISSHouseCooperationDetailViewController.h"
#import "SISSPassengerCooperationDetailViewController.h"
#import "SISSNetworkingManager+Home.h"
#import "SISSHouseCooperationDetailModel.h"
#import "SISSPassengerCooperationDetailModel.h"
#import "SISSPickerView.h"



@interface SISSCooperationDetailViewController ()
@property (weak, nonatomic) IBOutlet SISSTextView *cooperationDescriptionTextView;
@property (weak, nonatomic) IBOutlet UITextField *releaseLimitTextField;
@property (weak, nonatomic) IBOutlet UITextField *ownerComissonPercentTextField;
@property (weak, nonatomic) IBOutlet UILabel *partnerCommissionPercentLabel;

/** 房源合作范围 */
@property (assign, nonatomic) NSInteger cooperation_range; // 1.全行业2.公司。3门店

/** commissionPercentIndex */
@property (assign, nonatomic) NSInteger commissionPercentIndex;
/** 佣金比例 */
@property (assign, nonatomic) NSInteger commissionRate;

@end

@implementation SISSCooperationDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.cooperationDescriptionTextView.placeholder = @"请输入合作说明";
    self.cooperationDescriptionTextView.layer.borderWidth = 1;
    self.cooperationDescriptionTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    // same font
    self.cooperationDescriptionTextView.font = [UIFont systemFontOfSize:15.f];
    
    self.commissionPercentIndex = 50;
    self.commissionRate = 50;
}

/**
 选择发布合作范围点击事件
 */
- (IBAction)releaseLimitTapAction:(id)sender {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    alertController.view.tintColor = [UIColor blackColor];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
        
    }];
    [alertController addAction:cancelAction];
    
    NSArray *limitArr = @[@"全行业", @"公司", @"门店"];
    __weak typeof(self) weakSelf = self;
    for (NSInteger i = 0; i < limitArr.count; i++) {
        UIAlertAction *action = [UIAlertAction actionWithTitle:limitArr[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            weakSelf.cooperation_range = i + 1;
            weakSelf.releaseLimitTextField.text = limitArr[i];
            weakSelf.releaseLimitTextField.font = [UIFont systemFontOfSize:16];
        }];
        
        [alertController addAction:action];
    }

    [self presentViewController:alertController animated:YES completion:nil];
}

/**
 选择佣金百分比点击事件
 */
- (IBAction)partnerCommissionPercentClickAction:(id)sender {
    
    SISSPickerView *pickerView = [[SISSPickerView alloc] initWithFrame:self.view.bounds];
    pickerView.title = @"佣金百分比";
    NSMutableArray *titleArray = [NSMutableArray arrayWithCapacity:100];
    for (NSInteger i = 0; i < 100; i++) {
        
        [titleArray addObject:[NSString stringWithFormat:@"%zd",i]];
    }
    pickerView.titleArray = titleArray;
    pickerView.isArea = NO;
    pickerView.firstIndex = self.commissionPercentIndex;
    __weak typeof(self) weakself = self;
    pickerView.completion = ^(NSString *firstTitle, NSString *secondTitle, NSInteger firstIndex, NSInteger secondIndex) {
        
        weakself.ownerComissonPercentTextField.textColor = COLOR(0x334A60);
        weakself.ownerComissonPercentTextField.font = [UIFont systemFontOfSize:16];
        weakself.ownerComissonPercentTextField.text = [NSString stringWithFormat:@"%@%@",firstTitle,@"%"];
        weakself.partnerCommissionPercentLabel.text =[NSString stringWithFormat:@"%zd%@",100 - [firstTitle integerValue],@"%"];
        weakself.commissionRate = [firstTitle integerValue];
        weakself.commissionPercentIndex = firstIndex;
        
    };
}

/**
 发布按钮点击事件
 */
- (IBAction)releaseButtonClickAction:(id)sender {
    
    NSString *cooperation_desc = self.cooperationDescriptionTextView.text;
    
    if (self.releaseLimitTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择合作发布范围" andTime:1.0 andColor:YES];
        return;
    };
    
    if (self.ownerComissonPercentTextField.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请选择发布方佣金占比" andTime:1.0 andColor:YES];
        return;
    }
    
//    if (self.cooperationDescriptionTextView.text.length == 0) {
//        [SISSTools showSVErrorTitle:@"请输入合作说明" andTime:1.0 andColor:YES];
//        return;
//    };
    
    if (self.houseModel) {
        
        SISSHouseCooperationDetailModel *cooperationModel = [[SISSHouseCooperationDetailModel alloc] init];
        cooperationModel.cooperation_range = self.cooperation_range;
        cooperationModel.cooperation_insert_time = [NSDate yyr_dateCurrentTimeString]; // 获取系统的当前时间
        cooperationModel.commission_rate = self.commissionRate;
        cooperationModel.cooperation_desc = self.cooperationDescriptionTextView.text;
        
        //        NSString *str = [NSString unicodeToISO88591:cooperation_desc];
        
        NSDictionary *parameters = @{
                                     @"spId":[SISSUserAccount shared].spId,
                                     @"soId":[SISSUserAccount shared].soId,
                                     @"sobId":[SISSUserAccount shared].sobId,
                                     @"ahrId":self.houseModel.ID,
                                     @"cooperation_type":@(self.houseModel.houseresType),
                                     @"cooperation_range":@(self.cooperation_range),
                                     @"commission_rate":@(self.commissionRate),
                                     @"cooperation_desc":cooperation_desc
                                     };
        [kNetworkManager releaseHouseCooperationWithparameters:parameters success:^(id responseObject) {
            
            NSLog(@"发布房源合作: %@",responseObject);
            if ([responseObject[@"code"] integerValue] == 200) { // 发布成功
                
                SISSHouseCooperationDetailViewController *vc = [[SISSHouseCooperationDetailViewController alloc] init];
                vc.isFindCooperation = NO;
                vc.houseResourceModel = self.houseModel;
                vc.houseCooperationModel = cooperationModel;
                
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
        
    } else {
        
        SISSPassengerCooperationDetailModel *passengerCooperationModel = [[SISSPassengerCooperationDetailModel alloc] init];
        passengerCooperationModel.cooperation_insert_time = [NSDate yyr_dateCurrentTimeString];
        passengerCooperationModel.commission_rate = self.commissionRate;
        passengerCooperationModel.cooperation_desc = self.cooperationDescriptionTextView.text;
        
        NSDictionary *parameters = @{
                                     @"spId":[SISSUserAccount shared].spId,
                                     @"soId":[SISSUserAccount shared].soId,
                                     @"sobId":[SISSUserAccount shared].sobId,
                                     @"abdId":self.passengerModel.ID,
                                     @"cooperation_type":@(self.passengerModel.demandType),
                                     @"cooperation_range":@(self.cooperation_range),
                                     @"commission_rate":@(self.commissionRate),
                                     @"cooperation_desc":cooperation_desc
                                     };
        
        [kNetworkManager releasePassengerCooperationWithparameters:parameters success:^(id responseObject) {
            
            NSLog(@"发布客源合作: %@",responseObject);
            if ([responseObject[@"code"] integerValue] == 200) { // 发布成功
                SISSPassengerCooperationDetailViewController *vc = [[SISSPassengerCooperationDetailViewController alloc] init];
                vc.isFindCooperation = NO;
                vc.passengerModel = self.passengerModel;
                vc.passengerCooperationModel = passengerCooperationModel;
                [self.navigationController pushViewController:vc animated:YES];
            }
            
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
    }
}


@end
